<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<script type="text/javascript">
			var socket;
			
			if(!window.WebSocket){
				window.WebSocket = window.MozWebSocket;
			}

			if(window.WebSocket){
			socket = new WebSocket("ws://192.168.0.99:8082/websocket");
			
			socket.onmessage  =function(event){

				var ta = document.getElementById("responseText");
			
				ta.value=ta.value+"\n"+event.data;


			var str = event.data;
			var arr = JSON.parse(str);
			// tr = document.createElement("tr");
			// var html ="<td>编码</td><td>状态</td><td>标签码2</td>"
			
			// tr.innerHTML=html;
			// document.getElementById("tab").appendChild(tr);
			
			var html = "<tr><td>编号</td><td>主机码</td><td>编码</td><td>状态</td><td>标签码</td></tr>";
			
			for(var i = 0;i<arr.length;i++){
				var value = "<tr><td>"+(i+1)+"</td><td>"+arr[i].miniCode+"</td><td>"+arr[i].code+"</td><td>"+arr[i].status+"</td><td>"+arr[i].tag+"</td></tr>";
				
				html = html+value;
				
				
			}
			
			
			document.getElementById("tab").innerHTML=html;
			
			
			
			};
			
			socket.onopen = function(event){
				var ta=document.getElementById("responseText");
				ta.value=ta.value+"\n打开websocket服务正常";
			};
			socket.onclose = function(event){
				var ta = document.getElementById("responseText");
				
				ta.value = ta.value+"\nwebsocket关闭"
			};
		}else{
		alert("抱歉，你的浏览器不支持websocket");
		}
		
		function send(message){
				
				if(!window.WebSocket){
					return;
				}
				if(socket.readyState == WebSocket.OPEN){
					socket.send(message);
				}else{
					alert("websocket连接未建立")
				}
		}
		</script>
		
		<form onsubmit="return false;">
			模块：<input type="text" name="message" value="1"/>
			<input type="button" value="发送消息" onclick="send(this.form.message.value)" />
			<hr color="blue"/>
			<h3>服务器应答的消息</h3>
			
		
			<table border="1" id="tab">
				
				
			</table>
				<textarea id="responseText" style="width: 500px;height: 300px;">1</textarea>
		</form>
		
	</body>
</html>
